//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#ifndef __AURATYPES_H__
#define __AURATYPES_H__

//#include <elans.h>


//_ELASTOS_NAMESPACE_BEGIN
//-----------------------Aura basic types-----------------------
typedef char               Aura_Int8;
typedef short              Aura_Int16;
typedef long               Aura_Int32;
typedef long long          Aura_Int64;
typedef char               Aura_Char;
typedef float              Aura_Float;
typedef double             Aura_Double;

typedef unsigned char      Aura_UInt8;
typedef unsigned short     Aura_UInt16;
typedef unsigned long      Aura_UInt32;
typedef unsigned long long Aura_UInt64;
typedef unsigned char      Aura_UChar;
typedef unsigned char      Aura_Byte;

typedef unsigned short     Aura_WChar;
typedef void               Aura_Void;
typedef unsigned char      Aura_Bool;

typedef char               *Aura_PInt8;
typedef short              *Aura_PInt16;
typedef long               *Aura_PInt32;
typedef long long          *Aura_PInt64;
typedef char               *Aura_PChar;
typedef float              *Aura_PFloat;
typedef double             *Aura_PDouble;

typedef unsigned char      *Aura_PUInt8;
typedef unsigned short     *Aura_PUInt16;
typedef unsigned long      *Aura_PUInt32;
typedef unsigned long long *Aura_PUInt64;
typedef unsigned char      *Aura_PUChar;
typedef unsigned char      *Aura_PByte;

typedef unsigned short     *Aura_PWChar;
typedef void               *Aura_PVoid;


//-----------------------Aura extern types-----------------------
typedef Aura_Int32  Aura_ECode;

typedef Aura_Int32  Aura_ChannelId;
typedef Aura_PVoid  Aura_Handle;
typedef Aura_UInt32 Aura_Address;

typedef Aura_Int32 Aura_Microsecond;
typedef Aura_Int32 Aura_Millisecond32;
typedef Aura_Int32 Aura_ExitCode;
typedef Aura_Int32 Aura_QuitCode;
typedef Aura_Int32 Aura_ProcessId;
typedef Aura_Int32 Aura_ThreadId;
typedef Aura_Int32 Aura_MemorySize;
typedef Aura_Int32 Aura_EventState;
typedef struct {
    Aura_Int32 seconds;
    Aura_Int32 microseconds;
} Aura_SystemTime;

typedef Aura_Int32 Aura_ModuleLoadFlags;

typedef Aura_Int32 Aura_WaitResult;
typedef Aura_Int32 Aura_ThreadCreationFlags;
typedef Aura_Int32 Aura_ThreadPriorityRank;
typedef Aura_Int32 Aura_ThreadPriority;
typedef Aura_Int32 Aura_MemoryMapFlags;
typedef Aura_Int32 Aura_MemoryProtection;

//_ELASTOS_NAMESPACE_END

//_ELASTOS_NAMESPACE_USING

//Aura_WaitResult
enum {
    Aura_WaitResult_OK = 0x00000000,
    Aura_WaitResult_Interrupted = 0x00000001,
    Aura_WaitResult_TimedOut = 0x00000002,
};

//Aura_ThreadCreationFlags
enum {
    Aura_ThreadCreationFlag_Started = 0,
    Aura_ThreadCreationFlag_Unstarted = 4,
};

//Aura_ThreadPriorityRank
enum {
    Aura_ThreadPriorityRank_Idle = 0x00000001,
    Aura_ThreadPriorityRank_Normal = 0x00000002,
    Aura_ThreadPriorityRank_SoftRealTime = 0x00000003,
    Aura_ThreadPriorityRank_HardRealTime = 0x00000004,
};

//Aura_ThreadPriority
enum {
    Aura_ThreadPriority_Lowest = 0x00000000,
    Aura_ThreadPriority_BelowNormal = 0x000000ff,
    Aura_ThreadPriority_Normal = 0x000001ff,
    Aura_ThreadPriority_AboveNormal = 0x000002ff,
    Aura_ThreadPriority_Highest = 0x000003ff,
};

//Aura_MemoryMapFlags
enum {
    Aura_MemoryMap_Private = 0x00000001,
    Aura_MemoryMap_Shared = 0x00000002,
    Aura_MemoryMap_Fixed = 0x00000004,
    Aura_MemoryMap_Upgrow = 0x00000010,
    Aura_MemoryMap_Downgrow = 0x00000020,
};

//Aura_MemoryProtection
enum {
    Aura_MemoryProtection_Read = 0x00000001,
    Aura_MemoryProtection_Write = 0x00000002,
    Aura_MemoryProtection_Exec = 0x00000004,
    Aura_MemoryProtection_IO = 0x00000008,
    Aura_MemoryProtection_VMem = 0x00000018,
    Aura_MemoryProtection_RW = 0x00000003,
    Aura_MemoryProtection_RX = 0x00000005,
    Aura_MemoryProtection_RWX = 0x00000007,
    Aura_MemoryProtection_RWIO = 0x0000000b,
    Aura_MemoryProtection_RWVM = 0x0000001b,
};

//-----------------------Aura macro-----------------------

//stdcall, cdecl
#if defined(_MSC_VER)
/* nothing to do */
#elif defined(_GNUC) && defined(__i386__)
    #ifndef __stdcall
        #define __stdcall __attribute__((stdcall))
    #endif
    #ifndef __cdecl
        #define __cdecl __attribute__((cdecl))
    #endif
#else
    #ifdef __arm__
        #ifndef __stdcall
            #define __stdcall
        #endif
        #ifndef __cdecl
            #define __cdecl
        #endif
    #else
        #error Define your __cdecl and __stdcall
    #endif
#endif

#define AURAAPICALLTYPE   __cdecl

#define AURA_API        extern "C" Aura_ECode __cdecl
#define AURA_API_(type) extern "C" type       __cdecl

#undef va_list
#undef va_start
#undef va_end
#undef va_arg

#ifdef gnu

#define va_list __builtin_va_list
#define va_start(v, args) __builtin_stdarg_start(v, args)
#define va_end  __builtin_va_end
#define va_arg  __builtin_va_arg

#else

#define va_list unsigned char*
#define _INTSIZEOF(n)   ((sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1))
#define va_start(ap, v) (ap = (va_list)&v + _INTSIZEOF(v))
#define va_arg(ap, t)   (*(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)))
#define va_end(ap)      (ap = (va_list)0)

#endif

#ifndef _VA_LIST_DEFINED
#define _VA_LIST_DEFINED
#endif

#define AURA_NULL  (0)

#define AURA_TRUE  1
#define AURA_FALSE 0

#define AURA_SIGNALED   1
#define AURA_UNSIGNALED 0

#define AURA_INFINITE    (Aura_Millisecond32)-1

#define AURA_INVALID_PID ((Aura_ProcessId)-1)

#define AURA_INVALID_TID ((Aura_ThreadId)-1)

#define AURA_MAX_PATH      260     /* max length of full pathname */
#define AURA_MAX_DIR       256     /* max length of path component for windows */
#define AURA_MAX_NAME_LEN  256

#define ELAURA_PROCESS_PREFIX_NAME L"ElAuraProcess"


//-----------------------Aura error code-----------------------
#define AURA_NOERROR                    ((Aura_ECode)0)
#define AURA_E_FAIL                     ((Aura_ECode)-1)
#define AURA_E_INVALID_ARGUMENT         ((Aura_ECode)0x81260000L)
#define AURA_E_OUT_OF_MEMORY            ((Aura_ECode)0x81250000L)
#define AURA_E_NOT_IMPLEMENTED          ((Aura_ECode)0x80004001L)
#define AURA_E_TIMED_OUT                ((Aura_ECode)0x81100000L)
#define AURA_E_DOES_NOT_EXIST           ((Aura_ECode)0x810C0000L)
#define AURA_E_BUFFER_TOO_SMALL         ((Aura_ECode)0x81200000L)
#define AURA_E_ALREADY_EXIST            ((Aura_ECode)0x810D0000L)
#define AURA_E_PROCESS_ALREADY_EXITED   ((Aura_ECode)0x81050000L)
#define AURA_E_NOT_ENOUGH_ADDRESS_SPACE ((Aura_ECode)0x81170000L)
#define AURA_E_FILE_NOT_FOUND           ((Aura_ECode)0x811D0000L)
#define AURA_E_NOT_OWNER                ((Aura_ECode)0x81120000L)
#define AURA_E_INVALID_OPERATION        ((Aura_ECode)0x810F0000L)
#define AURA_E_PROCESS_STILL_ACTIVE     ((Aura_ECode)0x81020000L)
#define AURA_E_BAD_FILE_FORMAT          ((Aura_ECode)0x81180000L)
#define AURA_E_BAD_EXE_FORMAT           ((Aura_ECode)0x81190000L)
#define AURA_E_BAD_DLL_FORMAT           ((Aura_ECode)0x811A0000L)
#define AURA_E_NOT_SUPPORTED            ((Aura_ECode)0x811E0000L)
#define AURA_E_ACCESS_DENIED            ((Aura_ECode)0x81240000L)
#define AURA_E_THREAD_ABORTED           ((Aura_ECode)0x81210000L)
#define AURA_E_INTERRUPTED              ((Aura_ECode)0x81110000L)

#define AURA_FAILED(x)                  ((Aura_ECode) (x) < 0)
#define AURA_SUCCEED(x)                 ((Aura_ECode) (x) >= 0)

//Externed just for easy use

#ifndef EXTERN
    #define EXTERN      extern
#endif

#ifndef EXTERN_C
    #define EXTERN_C      extern "C"
#endif

#ifndef CDECL
    #define CDECL     __cdecl
#endif

#ifndef STDCALL
    #define STDCALL   __stdcall
#endif

#ifdef _GNUC
#define Aura_DebugBreak()    do { __asm("int $3"); } while(0)
#elif defined(_MSVC)
#define Aura_DebugBreak()    do { __asm int 3 } while(0)
#endif // _x86

#ifndef assert
#define assert(exp) \
                    do { \
                        if (exp) { \
                            (void)0; \
                        } else { \
                            Aura_DebugBreak(); \
                        } \
                     } while(0)
#endif // _DEBUG

typedef int ObjectType;

enum {
    ObjectType_MetaData       = 0,
    ObjectType_NamedMutex     = 1,
    ObjectType_NamedEvent     = 2,
    ObjectType_NamedCondition = 3,
    ObjectType_NamedRwLock    = 4,
    ObjectType_SharedMemory   = 5,
    ObjectType_Invalid        = 6
};

// -------------------Thread Context -------------------------
#define AURA_SIZE_OF_80387_REGISTERS         80

#define AURA_MAXIMUM_SUPPORTED_EXTENSION     512

typedef struct _AURA_FLOATING_SAVE_AREA
{
    Aura_UInt32   ControlWord;
    Aura_UInt32   StatusWord;
    Aura_UInt32   TagWord;
    Aura_UInt32   ErrorOffset;
    Aura_UInt32   ErrorSelector;
    Aura_UInt32   DataOffset;
    Aura_UInt32   DataSelector;
    Aura_Byte     RegisterArea[AURA_SIZE_OF_80387_REGISTERS];
    Aura_UInt32   Cr0NpxState;
} AURA_FLOATING_SAVE_AREA;

typedef struct _Aura_ThreadContext
{
    Aura_UInt32   ContextFlags;
    Aura_UInt32   Dr0;
    Aura_UInt32   Dr1;
    Aura_UInt32   Dr2;
    Aura_UInt32   Dr3;
    Aura_UInt32   Dr6;
    Aura_UInt32   Dr7;
    AURA_FLOATING_SAVE_AREA FloatSave;
    Aura_UInt32   SegGs;
    Aura_UInt32   SegFs;
    Aura_UInt32   SegEs;
    Aura_UInt32   SegDs;
    Aura_UInt32   Edi;
    Aura_UInt32   Esi;
    Aura_UInt32   Ebx;
    Aura_UInt32   Edx;
    Aura_UInt32   Ecx;
    Aura_UInt32   Eax;
    Aura_UInt32   Ebp;
    Aura_UInt32   Eip;
    Aura_UInt32   SegCs;
    Aura_UInt32   EFlags;
    Aura_UInt32   Esp;
    Aura_UInt32   SegSs;
    Aura_Byte     ExtendedRegisters[AURA_MAXIMUM_SUPPORTED_EXTENSION];
} Aura_ThreadContext;

#endif//__AURATYPES_H__
